package com.example.aidocengine.controller;

import com.example.aidocengine.common.result.ApiResponse;
import com.example.aidocengine.pojo.entity.PrimaryDeviceTerminology;
import com.example.aidocengine.pojo.entity.SecondaryDeviceTerminology;
import com.example.aidocengine.service.TerminologyImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/terminology")
public class TerminologyController {

    @Autowired
    private TerminologyImportService terminologyImportService;

    @PostMapping("/import")
    public ApiResponse<Void> importTerminologyLibrary(
            @RequestParam("file") MultipartFile file) {
        
        log.info("开始导入术语库文件：{}", file.getOriginalFilename());
        
        terminologyImportService.importTerminologyLibrary(file);
        
        return ApiResponse.success("术语库导入成功", (Void) null);
    }

    @PostMapping("/refresh-cache")
    public ApiResponse<Void> refreshTerminologyCache() {
        log.info("刷新术语缓存");
        
        terminologyImportService.refreshTerminologyCache();
        
        return ApiResponse.success("术语缓存刷新成功", (Void) null);
    }

    @GetMapping("/secondary")
    public ApiResponse<List<SecondaryDeviceTerminology>> getSecondaryTerms() {
        log.info("收到获取二次设备术语列表请求");
        
        List<SecondaryDeviceTerminology> terms = terminologyImportService.getSecondaryTerms();
        return ApiResponse.success("查询成功", terms);
    }

    @GetMapping("/primary")
    public ApiResponse<List<PrimaryDeviceTerminology>> getPrimaryTerms() {
        log.info("收到获取一次设备术语列表请求");
        
        List<PrimaryDeviceTerminology> terms = terminologyImportService.getPrimaryTerms();
        return ApiResponse.success("查询成功", terms);
    }
    
    @GetMapping("/primary/hierarchy")
    public ApiResponse<List<PrimaryDeviceTerminology>> getPrimaryTermsHierarchy() {
        log.info("收到获取一次设备术语层级结构列表请求");
        
        List<PrimaryDeviceTerminology> terms = terminologyImportService.getPrimaryTermsHierarchy();
        return ApiResponse.success("查询成功", terms);
    }
    
    @GetMapping("/primary/device-types")
    public ApiResponse<List<PrimaryDeviceTerminology>> getDeviceTypes() {
        log.info("收到获取设备类型列表请求");
        
        List<PrimaryDeviceTerminology> deviceTypes = terminologyImportService.getDeviceTypes();
        return ApiResponse.success("查询成功", deviceTypes);
    }
    
    @GetMapping("/primary/principles/{parentId}")
    public ApiResponse<List<PrimaryDeviceTerminology>> getPrinciplesByParentId(
            @PathVariable Long parentId) {
        log.info("收到获取设备编写原则列表请求，父ID：{}", parentId);
        
        List<PrimaryDeviceTerminology> principles = terminologyImportService.getPrinciplesByParentId(parentId);
        return ApiResponse.success("查询成功", principles);
    }
    
    @GetMapping("/primary/deleted")
    public ApiResponse<List<PrimaryDeviceTerminology>> getDeletedPrimaryTerms() {
        log.info("收到获取标记删除的一次设备术语列表请求");
        
        List<PrimaryDeviceTerminology> terms = terminologyImportService.getDeletedPrimaryTerms();
        return ApiResponse.success("查询成功", terms);
    }

    @PostMapping("/secondary")
    public ApiResponse<SecondaryDeviceTerminology> saveSecondaryTerm(
            @RequestBody SecondaryDeviceTerminology term) {
        
        log.info("收到保存二次设备术语请求：{}", term.getKeyword());
        
        SecondaryDeviceTerminology savedTerm = terminologyImportService.saveSecondaryTerm(term);
        return ApiResponse.success("保存成功", savedTerm);
    }

    @PostMapping("/primary")
    public ApiResponse<PrimaryDeviceTerminology> savePrimaryTerm(
            @RequestBody PrimaryDeviceTerminology term) {
        
        log.info("收到保存一次设备术语请求：{}", term.getDeviceType());
        
        PrimaryDeviceTerminology savedTerm = terminologyImportService.savePrimaryTerm(term);
        return ApiResponse.success("保存成功", savedTerm);
    }

    @DeleteMapping("/secondary/{id}")
    public ApiResponse<Void> deleteSecondaryTerm(
            @PathVariable Long id) {
        
        log.info("收到删除二次设备术语请求：{}", id);
        
        terminologyImportService.deleteSecondaryTerm(id);
        return ApiResponse.success("删除成功", (Void) null);
    }

    @DeleteMapping("/primary/{id}")
    public ApiResponse<Void> deletePrimaryTerm(
            @PathVariable Long id) {
        
        log.info("收到删除一次设备术语请求：{}", id);
        
        terminologyImportService.deletePrimaryTerm(id);
        return ApiResponse.success("删除成功", (Void) null);
    }
}